package ru.ssau.university.web.beans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import ru.ssau.university.ejb.api.UniversityServiceLocal;
import ru.ssau.university.persistence.entity.Department;
import ru.ssau.university.persistence.entity.Faculty;
import ru.ssau.university.persistence.entity.Teacher;
import ru.ssau.university.web.helper.ListWrapper;
import ru.ssau.university.web.util.WebUtil;

@ManagedBean(name = "enterTeacherBean")
@RequestScoped
public class EnterTeacherBean {

	@EJB
	private UniversityServiceLocal universityService;
	
	private ListWrapper<Faculty> faculties      = new ListWrapper<Faculty>();
	private ListWrapper<Department> departments = new ListWrapper<Department>();
	private ListWrapper<Teacher> teachers       = new ListWrapper<Teacher>();

	private String password;

	public String initAll() {
		updateFaculties();
		updateDepartments();
		updateTeachers();
		return null;
	}

	private void updateFaculties() {
		faculties.setItems(universityService.getFaculties());
	}
	
	private void updateDepartments() {
		if (faculties.getId() != null) {
			departments.setItems(universityService.getDepartmentsForFaculty(faculties.getId()));
		}
	}
	
	private void updateTeachers() {
		if (departments.getId() != null) {
			teachers.setItems(universityService.getTeacherByDepartment(departments.getId()));
		}
	}
	
	public String doLogin() {
		boolean r = WebUtil.getAuthBean().loginAsTeacher(teachers.getId(), password);
		if (r) {
			return "pretty:welcome";
		} else {
			WebUtil.putError("Password is not correct");
			return null;
		}
		
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public ListWrapper<Faculty> getFaculties() {
		return faculties;
	}

	public void setFaculties(ListWrapper<Faculty> faculties) {
		this.faculties = faculties;
	}

	public ListWrapper<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(ListWrapper<Department> departments) {
		this.departments = departments;
	}

	public ListWrapper<Teacher> getTeachers() {
		return teachers;
	}

	public void setTeachers(ListWrapper<Teacher> teachers) {
		this.teachers = teachers;
	}

}
